// pages/ranking/ranking.js
const app = getApp()
const qcloud = require('../../vendor/qcloud-weapp-client-sdk/index.js')
const config = require('../../config.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    switchState: 'person',
    uId: "",
    pageSize: 10,
    pageNum: 1,
    list: [],
    total: '',
    cur: 'person',
    curObject: {},
    showState: false,
    conteState:false,
    qualifyingNames:[
      "入门新手", "起步熟手", "坚韧黑铁", "顽强青铜", "傲气白银", 
      "聪慧黄金", "巧思白金", "睿智黑金", "无暇砖石", "智者大师", 
      "万卷宗师", "博学史诗", "智慧传说", "最强大脑"
    ],
    
    contestsList:[]
  },

  switchTab: function (e) {
    this.setData({
      pageNum: 1
    })

    this.setData({
      switchState: e.currentTarget.dataset.cur
    })

    this.setData({
      cur: e.currentTarget.dataset.cur
    })

    this[e.currentTarget.dataset.cur]()
  },
  //个人全行
  allbank() {
    this.setData({
      showState: false
    })

    this.getList(config.service.obtainQualifingPersonBankFocus).then((value) => {
      this.setData({
        list: value.qualifyingUserStatDTOs
      })

      this.setData({
        curObject: value.qualifyingSelfStatDTO
      })

      this.setData({
        total: value.total
      })

      if (!this.data.curObject) {
        this.setData({
          showState: false
        })

        return false;
      }

      if (!this.data.curObject || this.data.list.findIndex((items) => { return this.data.curObject.uId == items.uId }) > -1) {
        this.setData({
          showState: false
        })
      } else {
        this.setData({
          showState: true
        })
      }

      
    })


  },
  // 个人部门
  person() {
    this.setData({
      showState: false
    })

    
    this.getList(config.service.obtainQualifyingPersonDepartmentFocus).then((value) => {

      this.setData({
        list: value.qualifyingUserStatDTOs
      })

      this.setData({
        curObject: value.qualifyingSelfStatDTO
      })

      this.setData({
        total: value.total
      })

      if (!this.data.curObject) {
        this.setData({
          showState: false
        })

        return false;
      }

      if (!this.data.curObject || this.data.list.findIndex((items) => { return this.data.curObject.uId == items.uId }) > -1) {
        this.setData({
          showState: false
        })
      } else {
        this.setData({
          showState: true
        })
      }

      
    })


  },

  getList(url) {
    return new Promise((resolve, reject) => {
      qcloud.request({
        url: url,
        method: "POST",
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          'uId': this.data.uId,
          'pageSize': this.data.pageSize,
          'pageNum': this.data.pageNum
        },
        login: true,
        success: (response) => {
          let data = response.data
          if (data.code === 0) {
            
            let qualifyingUserStatDTOs = data.qualifyingUserStatDTOs

            this.setItem(data.qualifyingSelfStatDTO)

            data.qualifyingUserStatDTOs.map((item)=>{
              return this.setItem(item)
            })

            resolve(data)
          } else {
            reject(data.msg)
          }
        },
        fail: function (err) {
          reject(err)
        }
      })
    })
  },

  setItem(item) {
    let iarr = ['zero', 'one', 'two', 'three']
    // item.iclass = item.level == '15' ? 'three' : iarr[Number(item.levelStars)]
    item.iclass = iarr[Number(item.levelStars)]
    item.levelName = app.globalData.qualifyingNames[Number(item.level)-1]

    return item
  },

  contestsFn(){
    this.setData({
      conteState:  true
    })
  },
  
  deleteFn() {
    this.setData({
      conteState: false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      contestsList: this.data.qualifyingNames.reverse()
    })

    wx.setNavigationBarTitle({
      title: '排行榜'
    })

    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          offsetH: res.windowHeight
        })
      }
    })


    this.setData({
      uId: app.globalData.uId || ''
    })

    this.person()

  },

  //下拉滚动加载
  lower() {
    
    this.setData({
      pageNum: this.data.pageNum + 1
    })

    if (this.data.pageNum > Math.ceil(this.data.total / 10)) return false;

    let serverUrl = {
      'person': config.service.obtainQualifyingPersonDepartmentFocus,
      'allbank': config.service.obtainQualifingPersonBankFocus
    }

    this.getList(serverUrl[this.data.cur]).then((value) => {

      this.setData({
        list: this.data.list.concat(value.qualifyingUserStatDTOs)
      })

      this.setData({
        curObject: value.qualifyingSelfStatDTO
      })

      if (this.data.list.findIndex((items) => { return this.data.curObject.uId == items.uId }) > -1) {
        this.setData({
          showState: false
        })
      } else {
        this.setData({
          showState: true
        })
      }

      this.setData({
        total: value.total
      })
    })
  }
})